package club.calong.sanniu.pojo;

import club.calong.sanniu.pojo.type.IntegerSetConverter;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.*;

@TableName("user")
public class User implements Serializable, UserDetails {

    public static final String ID = "_id";
    public static final String USERNAME = "username";
    public static final String ROLE_NAME = "roles";
    public String openid;
    public String phone;
    public boolean activated;

    public boolean getActivated () {
        return activated;
    }

    public void setActivated(boolean activated) {
        this.activated = activated;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @TableId(value = ID, type = IdType.AUTO)
    private String id;
    private String avatar;

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    public String getOpenid() {
        return openid;
    }

    private String introduction;
    @TableField("_name")
    private String name;

    @TableField(value = "roles", typeHandler = IntegerSetConverter.class)
    private Set<Integer> roles = new HashSet<>(10);

    @TableField(exist = false)
    private List<String> authorities;

    @TableField(exist = false)
    private Set<String> roleNames =  new HashSet<>(10);

    @JsonIgnore
    @TableField(exist = false)
    private Set<Integer> permissions = new HashSet<>(10);

    @TableField(exist = false)
    private Set<String> permissionNames = new HashSet<>(10);

    public Set<String> getPermissionNames() {
        return permissionNames;
    }

    public void setPermissionNames(Set<String> permissionNames) {
        this.permissionNames = permissionNames;
    }

    public Set<Integer> getPermissions() {
        return permissions;
    }

    public void setPermissions(Set<Integer> permissions) {
        this.permissions = permissions;
    }

    private String username;

    @TableField("_password")
    @JsonIgnore
    private String password;

    @TableField("_lock")
    private Boolean lock;

    private String email;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Boolean getLock() {
        return lock;
    }

    public void setLock(Boolean lock) {
        this.lock = lock;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getIntroduction() {
        return introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<Integer> getRoles() {
        return roles;
    }

    public void setRoles(Set<Integer> roles) {
        this.roles = roles;
    }

    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return !this.lock;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (CollectionUtils.isEmpty(authorities)) {
            return Collections.emptyList();
        }
        // 组装权限
        ArrayList<GrantedAuthority> auths = new ArrayList<>(authorities.size() * 2);
        authorities.forEach(auth -> {
            auths.add(new SimpleGrantedAuthority(auth));
        });
        return auths;
    }

    public void setAuthorities(List<String> authorities) {
        this.authorities = authorities;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Set<String> getRoleNames() {
        return roleNames;
    }

    public void setRoleNames(Set<String> roleNames) {
        this.roleNames = roleNames;
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", avatar='" + avatar + '\'' +
                ", introduction='" + introduction + '\'' +
                ", name='" + name + '\'' +
                ", roles=" + roles +
                ", roleNames=" + roleNames +
                ", authorities=" + authorities +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public void addRoleName(String name) {

        roleNames.add(name);
    }
}
